package com.hgkj.common.core.domain;

import com.hgkj.common.core.constant.HttpStatusConstant;
import org.springframework.util.ObjectUtils;

import java.util.Collection;
import java.util.HashMap;

/**
 * 统一信息返回类
 */
public class ApiResult extends HashMap<String, Object> {

    public static final int SUCCESS = HttpStatusConstant.SUCCESS;
    public static final int FAIL = HttpStatusConstant.ERROR;

    public static final String CODE = "code";
    public static final String MSG = "msg";
    public static final String DATA = "data";

    private ApiResult() {

    }

    private ApiResult(int code, String msg) {
        super.put(CODE, code);
        super.put(MSG, msg);
    }

    private ApiResult(int code, String msg, Object data) {
        super.put(CODE, code);
        super.put(MSG, msg);
        if (!ObjectUtils.isEmpty(data) || data.getClass().isArray() || data instanceof Collection) {
            super.put(DATA, data);
        }
    }

    public static ApiResult success() {
        return new ApiResult(SUCCESS, "操作成功");
    }

    public static ApiResult success(String msg) {
        return new ApiResult(SUCCESS, msg);
    }

    public static ApiResult success(Object data) {
        return new ApiResult(SUCCESS, "操作成功", data);
    }

    public static ApiResult success(String msg, Object data) {
        return new ApiResult(SUCCESS, msg, data);
    }

    /**
     * 失败响应
     */

    public static ApiResult error() {
        return new ApiResult(FAIL,"操作失败");
    }
    public static ApiResult error(Object data) {
        return new ApiResult(FAIL,"操作失败",data);
    }

    public static ApiResult error(String msg) {
        return new ApiResult(FAIL, msg);
    }

    public static ApiResult error(int code,String msg) {
        return new ApiResult(code, msg);
    }

    public static ApiResult error(String msg, Object data) {
        return new ApiResult(FAIL, msg, data);
    }


}
